package com.example.calculator

import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.NavigationUI

class MainActivity : AppCompatActivity() {
    private lateinit var controller:NavController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        controller=Navigation.findNavController(this, R.id.fragment)
        NavigationUI.setupActionBarWithNavController(this,controller)
    }

    override fun onSupportNavigateUp(): Boolean {
        //表示返回键点击的当前页面为questionFragment
        if (controller.currentDestination!!.id==R.id.questionFragment){
            var builder:AlertDialog.Builder=AlertDialog.Builder(this)
            builder.setTitle(getString(R.string.quit_msg))
            builder.setPositiveButton(getString(R.string.possitive_button), DialogInterface.OnClickListener { dialogInterface: DialogInterface, i: Int ->
                controller.navigateUp()
            })
            builder.setNegativeButton(getString(R.string.negative_button), DialogInterface.OnClickListener { dialog, which ->

            })
            val dialog=builder.create()
            dialog.show()

        }else if (controller.currentDestination!!.id==R.id.titleFragment){
            finish()
        }else{
            controller.navigate(R.id.titleFragment)
        }
        return super.onSupportNavigateUp()
    }

    override fun onBackPressed() {
//        super.onBackPressed()
        onSupportNavigateUp()
    }
}
